from rest_framework.permissions import BasePermission, OR
from rest_framework import HTTP_HEADER_ENCODING

from mysqldb.models import (
    AllinpayUserUserList,
    AllinpayUserPermissViews,
    AllinpayAuthViews,
    TankUsernameList,
    TankUserPermissViews,
)


class AllPermissions(BasePermission):
    """允许所有权限"""

    def has_permission(self, request, view):
        return True

    def has_object_permission(self, request, view, obj):
        return True


class MyPermissions(BasePermission):
    """允许所有权限"""

    def has_permission(self, request, view):
        # 验证登录
        if not request.user:
            return False  # 禁止未登录访问

        return True

    def has_object_permission(self, request, view, obj):
        # 验证登录
        if not request.user:
            return False  # 禁止未登录访问

        return True


class AdminPermissions(BasePermission):
    """管理员权限"""

    def has_permission(self, request, view):
        # 验证登录
        if not request.user:
            return False  # 禁止未登录访问

        return True

    def has_object_permission(self, request, view, obj):
        # 验证登录
        if not request.user:
            return False  # 禁止未登录访问

        return True


class UserPermissions(BasePermission):
    """用户权限"""

    table = AllinpayUserUserList
    perms = AllinpayUserPermissViews

    def has_name(self, view):
        """返回类名"""
        n = repr(view).split(" ")[0].split(".")[-1]
        return n

    def validateUserViews(self, uid, name, act):
        """验证用户视图权限"""
        try:
            v = AllinpayAuthViews.objects.get(name=name, act=act)
            r = self.perms.objects.get(userid=uid, viewid=v.id, status=1)
        except BaseException as err:
            return False
        else:
            return True

    def has_permission(self, request, view):
        # 验证登录
        if not request.user:
            return False  # 禁止未登录访问
        # 验证类型
        if not isinstance(request.user, self.table):
            return False

        return True

        # 格式化类名
        info = self.has_name(view)
        # 验证用户视图权限
        isContinue = self.validateUserViews(
            request.user.id, info, request.method.upper()
        )
        return isContinue

    def has_object_permission(self, request, view, obj):
        # 验证登录
        return True

        if not request.user:
            return False  # 禁止未登录访问

        return True


class TankPermissions(BasePermission):
    """Tank权限"""

    table = TankUsernameList
    perms = TankUserPermissViews

    def has_name(self, view):
        """返回类名"""
        n = repr(view).split(" ")[0].split(".")[-1]
        return n

    def validateUserViews(self, uid, name, act):
        """验证用户视图权限"""
        try:
            v = AllinpayAuthViews.objects.get(name=name, act=act)
            r = self.perms.objects.get(userid=uid, viewid=v.id, status=1)
        except BaseException as err:
            return False
        else:
            return True

    def has_permission(self, request, view):
        # 验证登录
        if not request.user:
            return False  # 禁止未登录访问
        # 验证类型
        if not isinstance(request.user, self.table):
            return False

        return True

        # 格式化类名
        info = self.has_name(view)
        # 验证用户视图权限
        isContinue = self.validateUserViews(
            request.user.id, info, request.method.upper()
        )
        return isContinue

    def has_object_permission(self, request, view, obj):
        # 验证登录
        return True

        if not request.user:
            return False  # 禁止未登录访问

        return True
